package com.baizhi.cmfzdemo.service.impl;

import com.baizhi.cmfzdemo.service.CmfzUserService;
import com.baizhi.cmfzdemo.service.UserThreadService;
import com.baizhi.cmfzdemo.util.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * Created by 54433 on 2019/4/8.
 */
@Service
public class UserThreadServcieImpl implements UserThreadService{
    @Autowired
    private CmfzUserService cmfzUserService;

    @Override
    public Map selectAllCountThread() throws InterruptedException {
        Map map = new HashMap<>();
        //1.创建计数器
        CountDownLatch countDownLatch = new CountDownLatch(3);
        //2.开启三个任务线程
        ThreadPollUtil.executor.execute(new Runnable(){

            @Override
            public void run() {
                //查询注册量
                try{
                    Map map1 = cmfzUserService.selectByDayCount();
                    map.put("count",map1);
                }catch (Exception e){
                    countDownLatch.countDown();
                }

            }
        });
        ThreadPollUtil.executor.execute(new Runnable(){

            @Override
            public void run() {
                //查询男女比例
                List<Map> maps = cmfzUserService.queryBySex();
                map.put("sex",maps);
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.execute(new Runnable(){
            @Override
            public void run() {
                //查询地区分布
                List<Map> maps = cmfzUserService.queryByProvince();
                map.put("province",maps);
                countDownLatch.countDown();
            }
        });
//使用await'（）方法  让线程等待任务线程执行结束
        countDownLatch.await();
        return map;
    }

    @Override
    public Map selectAllCountFuture() throws Exception {
        Map map = new HashMap<>();
        Future<String> future1 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Map map1 = cmfzUserService.selectByDayCount();
                map.put("count",map1);
//                计数器减一
                return "haha";
            }
        });

        Future<String> future2 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = cmfzUserService.queryBySex();
                map.put("sex",maps);
                return "haha";
            }
        });

        Future<String> future3 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = cmfzUserService.queryByProvince();
                map.put("province",maps);
                return "haha";
            }
        });

        if ("haha".equals(future1.get()) && "haha".equals(future2.get()) && "haha".equals(future3.get())){
            return map;
        }

        return map;
    }

    @Override
    //没有优化的
    public Map selectAllCount() {
        Map map = new HashMap<>();
//        注册量
        Map map1 = cmfzUserService.selectByDayCount();
//        地区分布
        List<Map> maps = cmfzUserService.queryByProvince();
//        男女比例
        List<Map> maps1 = cmfzUserService.queryBySex();
        map.put("sex",maps1);
        map.put("count",map1);
        map.put("province",maps);
        return map;
    }
}
